package com.lancoo.examview.exam;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.bean.ExamViewBean;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.model.eventBean.AnswerSheetEventBean;
import com.lancoo.answer.view.result.ExamResultFragment;
import com.lancoo.examview.R;
import com.lancoo.examview.exam.bean.ResponceBean;
import com.lancoo.examview.utils.ConvertStremUtils;
import com.rxjava.rxlife.RxLife;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ScoreCountActivity extends AppCompatActivity {
    public static void launch(Context context, String dataAsset, int trainSence) {
        Intent intent = new Intent(context, ScoreCountActivity.class);
        intent.putExtra("dataAsset", dataAsset);
        intent.putExtra("trainSence", trainSence);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_count);

        EventBus.getDefault().register(this);

        Observable.create((ObservableOnSubscribe<List<Type>>) emitter -> {
                    String dataAsset = getIntent().getStringExtra("dataAsset");
                    String originalFundData = ConvertStremUtils.convertStremToString(this, dataAsset);
                    Gson gson = new Gson();
                    ResponceBean paper = gson.fromJson(originalFundData, ResponceBean.class);
                    List<Type> typeList = paper.getData().getPaperData();
                    // ConvertStremUtils.reBuildTypeList(typeList);
                    int trainSence = getIntent().getIntExtra("trainSence", 1);
                    TaskControlBean controlBean = new TaskControlBean();
                    controlBean.setTaskState(TaskState.LOOKING);
                    controlBean.setTrainSence(trainSence);

                    ExamViewBean examViewBean = new ExamViewBean();
                    examViewBean.setScore("12");
                    examViewBean.setHighestScore("56");
                    examViewBean.setDuration("用时 10'9''");
                    examViewBean.setTypeList(typeList);

                    ConstantBean.Companion.setConstantExamBean(examViewBean);
                    ConstantBean.Companion.setTaskControlBean(controlBean);
                    emitter.onNext(typeList);
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(RxLife.as(this))
                .subscribe(types -> {
                    ExamResultFragment fragment = new ExamResultFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.layout_content, fragment, "myTag")
                            .commitAllowingStateLoss();
                }, throwable -> {});

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAnswerSheetEvent(final AnswerSheetEventBean eventBean) {
        Toast.makeText(
                getApplicationContext(),
                eventBean.getTypeIndex() + ", " + eventBean.getQuesIndex() + ", " + eventBean.getChildIndex() + ", " + eventBean.getItemIndex(),
                Toast.LENGTH_SHORT
        ).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
